import 'package:flutter/material.dart';
import 'package:flutter_axrjc/datasource/network/bean/TaskBean.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:get/get.dart';

import 'logic.dart';

class AttendancePage extends StatelessWidget {
  const AttendancePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<AttendanceLogic>();
    final state = Get.find<AttendanceLogic>().state;

    return Scaffold(
      backgroundColor: const Color(0xffF3F6F9),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Stack(
              children: [
                Column(
                  children: [
                    Stack(
                      children: [
                        Container(
                          height: 237,
                          width: Get.width,
                          decoration: const BoxDecoration(
                              gradient: LinearGradient(
                                  begin: Alignment.topLeft,
                                  end: Alignment.bottomRight,
                                  colors: [
                                Color(0xffF7D25D),
                                Color(0xffE3801D),
                              ])),
                        ),
                        Positioned(
                            left: 10,
                            right: 10,
                            top: 0,
                            child: SafeArea(
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  GestureDetector(
                                    onTap: Get.back,
                                    child: const Padding(
                                      padding: EdgeInsets.all(8.0),
                                      child: Icon(
                                        Icons.arrow_back_ios_new,
                                        color: Colors.white,
                                      ),
                                    ),
                                  ),
                                  // Padding(
                                  //   padding: const EdgeInsets.all(8.0),
                                  //   child: Column(
                                  //     children: [
                                  //       Image.asset(
                                  //         'assets/main/qiandao.png',
                                  //         scale: 2,
                                  //         color: Colors.white,
                                  //       ),
                                  //       const Text(
                                  //         "签到",
                                  //         style: TextStyle(
                                  //             fontSize: 12,
                                  //             color: Colors.white),
                                  //       )
                                  //     ],
                                  //   ),
                                  // ),
                                ],
                              ),
                            )),
                        Positioned(
                            top: 100,
                            bottom: 0,
                            right: 0,
                            child: Image.asset('assets/main/task1.png')),
                        Positioned(
                            top: 10,
                            left: 0,
                            right: 0,
                            child: SafeArea(
                              child: Column(
                                children: [
                                  const Center(
                                    child: Text(
                                      "任务中心",
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 16),
                                    ),
                                  ),
                                  const SizedBox(height: 30),
                                  Stack(
                                    children: [
                                      Row(
                                        children: [
                                          Stack(
                                            children: [
                                              Image.asset(
                                                'assets/main/jifen.png',
                                                scale: 3,
                                                fit: BoxFit.cover,
                                              ),
                                              const Positioned.fill(
                                                  child: Center(
                                                child: Text(
                                                  "我的积分",
                                                  style: TextStyle(
                                                      color: Colors.white),
                                                ),
                                              ))
                                            ],
                                          ),
                                        ],
                                      ),
                                      Positioned.fill(
                                        child: Center(
                                          child: Obx(() {
                                            return Text(
                                              state.userIntegral.value,
                                              style: const TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                  color: Colors.white,
                                                  fontSize: 30),
                                            );
                                          }),
                                        ),
                                      )
                                    ],
                                  )
                                ],
                              ),
                            )),
                      ],
                    ),
                    const SizedBox(
                      height: 120,
                    ),
                  ],
                ),
                Positioned(
                  bottom: 0,
                  left: 10,
                  right: 10,
                  child: Container(
                      height: 150,
                      padding: const EdgeInsets.all(10),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10)),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Obx(() {
                            return Text(
                              "已连续签到${state.day.value}天",
                              style: const TextStyle(fontSize: 16),
                            );
                          }),
                          Expanded(
                            child: Obx(() {
                              if (state.qianDaoList.isNotEmpty == true) {
                                return ListView.builder(
                                  scrollDirection: Axis.horizontal,
                                  itemCount: state.qianDaoList.length,
                                  itemBuilder:
                                      (BuildContext context, int index) {
                                    var bean = state.qianDaoList[index];
                                    return InkWell(
                                      onTap: () {
                                        if (index == 0) {
                                          logic.qiandao(bean);
                                        }
                                      },
                                      child: Padding(
                                        padding: const EdgeInsets.all(5),
                                        child: Center(
                                          child: Container(
                                              padding: const EdgeInsets.all(5),
                                              decoration: BoxDecoration(
                                                  borderRadius:
                                                      BorderRadius.circular(8),
                                                  color: bean.flag == "1"
                                                      ? primaryColor
                                                          .withAlpha(51)
                                                      : const Color(0xFFDCDCDC)
                                                          .withAlpha(51)),
                                              child: Column(
                                                mainAxisSize: MainAxisSize.min,
                                                children: [
                                                  const CircleAvatar(
                                                    radius: 12.5,
                                                    backgroundColor:
                                                        Color(0xffFFD96B),
                                                    child: CircleAvatar(
                                                      radius: 7,
                                                      backgroundColor:
                                                          Color(0xffFFBE00),
                                                    ),
                                                  ),
                                                  const SizedBox(
                                                    height: 5,
                                                  ),
                                                  Text("${bean.day}"),
                                                ],
                                              )),
                                        ),
                                      ),
                                    );
                                  },
                                );
                              } else {
                                return Container();
                              }
                            }),
                          ),
                          Text(
                            "周一开始连续签到满7天，最后一日+100积分",
                            style: TextStyle(color: orangeColor.withAlpha(150)),
                          ),
                        ],
                      )),
                )
              ],
            ),
            const SizedBox(
              height: 20,
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: Obx(() {
                return Text(
                  "我的任务(${state.taskList.length})",
                  style: const TextStyle(fontSize: 16),
                );
              }),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 10),
              child: Obx(() {
                if (state.taskList.isEmpty) {
                  return const Center(child: Text("暂无任务"));
                }
                return ListView.separated(
                  padding: const EdgeInsets.symmetric(vertical: 15),
                  itemCount: state.taskList.length,
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (BuildContext context, int index) {
                    var bean = state.taskList[index];
                    return InkWell(
                      onTap: () {
                        logic.jump(bean);
                      },
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 5),
                        child: Row(
                          children: [
                            Expanded(
                                child: Row(
                              children: [
                                (bean.taskPic?.isEmpty == true ||
                                        bean.taskPic == null)
                                    ? const CircleAvatar(
                                        radius: 25,
                                        backgroundImage: AssetImage(
                                          "assets/main/jifen.png",
                                        ))
                                    : SizedBox(
                                        width: 30,
                                        height: 30,
                                        child: Image.network(
                                          "${bean.taskPic}",
                                          fit: BoxFit.cover,
                                        ),
                                      ),
                                const SizedBox(
                                  width: 8,
                                ),
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      "${bean.taskTitle} (${getCustomNum(bean)}/${bean.taskNum})",
                                      style: const TextStyle(fontSize: 14),
                                    ),
                                    Text(
                                      bean.taskDesc?.isEmpty == true
                                          ? '暂无任务描述'
                                          : "${bean.taskDesc?.trim()}",
                                      style:
                                          const TextStyle(color: Colors.grey),
                                    ),
                                  ],
                                )
                              ],
                            )),
                            Row(
                              children: [
                                const CircleAvatar(
                                  radius: 12.5,
                                  backgroundColor: Color(0xffFFD96B),
                                  child: CircleAvatar(
                                    radius: 7,
                                    backgroundColor: Color(0xffFFBE00),
                                  ),
                                ),
                                Padding(
                                  padding:
                                      const EdgeInsets.symmetric(horizontal: 5),
                                  child: Text("${bean.taskIntegral}"),
                                ),
                                bean.ifFinish == "0"
                                    ? Container(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 15, vertical: 6),
                                        decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(20),
                                            color: primaryColor),
                                        child: const Text(
                                          "去完成",
                                          style: TextStyle(fontSize: 12),
                                        ))
                                    : Container(
                                        padding: const EdgeInsets.symmetric(
                                            horizontal: 15, vertical: 6),
                                        decoration: BoxDecoration(
                                            borderRadius:
                                                BorderRadius.circular(20),
                                            color: Colors.grey.shade300),
                                        child: const Text(
                                          "已完成",
                                          style: TextStyle(fontSize: 12),
                                        )),
                              ],
                            )
                          ],
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) {
                    return const SizedBox(
                      height: 10,
                    );
                  },
                );
              }),
            )
          ],
        ),
      ),
    );
  }

  String getCustomNum(TaskBean bean) {
    return (int.tryParse("${bean.customerNum}") ?? 0) >
            (int.tryParse("${bean.taskNum}") ?? 0)
        ? "${bean.taskNum}"
        : "${bean.customerNum}";
  }
}
